<?php namespace Home\Controller;
use \Hdphp\Controller\Controller;
use \Common\Model\User;
class MemberController extends Controller{
//	会员中心
	public function index(){
		View::make();
	}

//	注册页面
	public function reg(){
		if(IS_AJAX){
            $userModel = new User();
			$password=Q('post.password');
			$queren=Q('post.queren');
			if($password!==$queren){
				$this->ajax(['status'=>false,'message'=>'两次输入不一致']);
			}
            if(!$userModel->store()){
                $this->ajax(['status'=>false,'message'=>$userModel->getError()]);
            }else{
                $this->ajax(['status'=>true,'message'=>'注册成功']);
                echo "location.href='U(Index/index)'"; 
            }
        }
		View::make();
	}
//	登陆页面
	public function login(){
		if(IS_AJAX){
			$userModel = new User();
			$username=Q('post.username');
			$password=Q('post.password','','md5');
			$uid=$userModel->where("username='{$username}'")->pluck('uid');
			$code=Q('post.code','','strtolower');
			if(!$username){
				$this->ajax(['status'=>false,'message'=>'用户名必填']);				
			}
			if(!$password){
				$this->ajax(['status'=>false,'message'=>'密码必填']);
			}
			if(!$code){
				$this->ajax(['status'=>false,'message'=>'验证码必填']);
			}
			$oldUsername=$userModel->where("username='{$username}'")->find();
			if(!$oldUsername){
				$this->ajax(['status'=>false,'message'=>'用户名不存在']);
			}
			$oldPassword=$userModel->where("password='{$password}'")->find();
			if(!$oldPassword){
				$this->ajax(['status'=>false,'message'=>'密码不正确']);
			}
			if(!$code==strtolower($_SESSION['code'])){
				$this->ajax(['status'=>false,'message'=>'验证码不正确']);
			}
			$_SESSION['uid']=$uid;
			$_SESSION['username']=$username;
			$this->ajax(['status'=>true,'message'=>'登陆成功']);
		}
		
		View::make();
	}
	public function code(){
		Code::make();
	}
	public function out(){
		unset($_SESSION['uid']);
		View::success('退出成功',U('Index/index'));
	}
}
